.TH std::shared_future 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::shared_future \- std::shared_future

.SH Synopsis
   Defined in header <future>
   template< class T > class shared_future;     \fB(1)\fP \fI(since C++11)\fP
   template< class T > class shared_future<T&>; \fB(2)\fP \fI(since C++11)\fP
   template<> class shared_future<void>;        \fB(3)\fP \fI(since C++11)\fP

   The class template std::shared_future provides a mechanism to access the result of
   asynchronous operations, similar to std::future, except that multiple threads are
   allowed to wait for the same shared state. Unlike std::future, which is only
   moveable (so only one instance can refer to any particular asynchronous result),
   std::shared_future is copyable and multiple shared future objects may refer to the
   same shared state.

   Access to the same shared state from multiple threads is safe if each thread does it
   through its own copy of a shared_future object.

.SH Member functions

   constructor   constructs the future object
                 \fI(public member function)\fP
   destructor    destructs the future object
                 \fI(public member function)\fP
   operator=     assigns the contents
                 \fI(public member function)\fP
.SH Getting the result
   get           returns the result
                 \fI(public member function)\fP
.SH State
   valid         checks if the future has a shared state
                 \fI(public member function)\fP
   wait          waits for the result to become available
                 \fI(public member function)\fP
                 waits for the result, returns if it is not available for the specified
   wait_for      timeout duration
                 \fI(public member function)\fP
                 waits for the result, returns if it is not available until specified
   wait_until    time point has been reached
                 \fI(public member function)\fP

.SH Example

   A shared_future may be used to signal multiple threads simultaneously, similar to
   std::condition_variable::notify_all().


// Run this code

 #include <chrono>
 #include <future>
 #include <iostream>

 int main()
 {
     std::promise<void> ready_promise, t1_ready_promise, t2_ready_promise;
     std::shared_future<void> ready_future(ready_promise.get_future());

     std::chrono::time_point<std::chrono::high_resolution_clock> start;

     auto fun1 = [&, ready_future]() -> std::chrono::duration<double, std::milli>
     {
         t1_ready_promise.set_value();
         ready_future.wait(); // waits for the signal from main()
         return std::chrono::high_resolution_clock::now() - start;
     };


     auto fun2 = [&, ready_future]() -> std::chrono::duration<double, std::milli>
     {
         t2_ready_promise.set_value();
         ready_future.wait(); // waits for the signal from main()
         return std::chrono::high_resolution_clock::now() - start;
     };

     auto fut1 = t1_ready_promise.get_future();
     auto fut2 = t2_ready_promise.get_future();

     auto result1 = std::async(std::launch::async, fun1);
     auto result2 = std::async(std::launch::async, fun2);

     // wait for the threads to become ready
     fut1.wait();
     fut2.wait();

     // the threads are ready, start the clock
     start = std::chrono::high_resolution_clock::now();

     // signal the threads to go
     ready_promise.set_value();

     std::cout << "Thread 1 received the signal "
               << result1.get().count() << " ms after start\\n"
               << "Thread 2 received the signal "
               << result2.get().count() << " ms after start\\n";
 }

.SH Possible output:

 Thread 1 received the signal 0.072 ms after start
 Thread 2 received the signal 0.041 ms after start

.SH See also

   async   runs a function asynchronously (potentially in a new thread) and returns a
   \fI(C++11)\fP std::future that will hold the result
           \fI(function template)\fP
   future  waits for a value that is set asynchronously
   \fI(C++11)\fP \fI(class template)\fP
